﻿app.factory('WeatherService', ['$http', '$q', function ($http, $q) {
    
    var loadWeatherInfo = function () {
        var deferred = $q.defer();

        var promises = [];

        promises.push($http.get('http://api.openweathermap.org/data/2.5/weather?q=Pearland,tx&units=imperial'));
        promises.push($http.get('http://api.openweathermap.org/data/2.5/weather?q=Edenton,nc&units=imperial'));
        promises.push($http.get('http://api.openweathermap.org/data/2.5/weather?q=north%20arlington,%20nj&units=imperial'));
        promises.push($http.get('http://api.openweathermap.org/data/2.5/weather?q=meadville,pa&units=imperial'));
        promises.push($http.get('http://api.openweathermap.org/data/2.5/weather?q=Austin,tx&units=imperial'));

        var count = 0;
        promises.forEach(function (p) {            
            p.then(function () {
                count++;
                deferred.notify(count);
            });
        });

        $q.all(promises)
        .then(function (data) {
            deferred.resolve(data);
        }, function () {
            deferred.reject();
        }, function (update) {
            deferred.notify(update);
        });

        return deferred.promise;
    }

    return {
        loadWeatherInfo: loadWeatherInfo
    };
}]);